package com.hddz.comlib.ui.base;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.hddz.comlib.R;
import com.hddz.comlib.util.BaseUtility;

public class GalleryActivity extends Activity {
	public int position = 0; // 当前显示图片的位置
	public ArrayList<String> photos;
	private String fdir;

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);
		fdir = BaseUtility.getSDPath() + "/rfidphotos";

		myGallery galllery = (myGallery) findViewById(R.id.mygallery);
		Intent intent = getIntent();
		position = intent.getIntExtra("position", 0); // 获取GridViewActivity传来的图片位置position
		photos = (ArrayList<String>) intent.getExtras().get("photos");
		ImageAdapter imgAdapter = new ImageAdapter(this);
		galllery.setAdapter(imgAdapter); // 设置图片ImageAdapter
		galllery.setSelection(position); // 设置当前显示图片

		Animation an = AnimationUtils.loadAnimation(this, R.anim.scale); // Gallery动画
		galllery.setAnimation(an);
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private int mPos;

		public ImageAdapter(Context context) {
			mContext = context;
		}

		public void setOwnposition(int ownposition) {
			this.mPos = ownposition;
		}

		public int getOwnposition() {
			return mPos;
		}

		@Override
		public int getCount() {
			return photos.size();
		}

		@Override
		public Object getItem(int position) {
			mPos = position;
			return position;
		}

		@Override
		public long getItemId(int position) {
			mPos = position;
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			mPos = position;
			ImageView imageview = new ImageView(mContext);
			imageview.setBackgroundColor(0xFF000000);
			imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
			imageview.setLayoutParams(new myGallery.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT));
			Bitmap bitmap = BitmapFactory.decodeFile(fdir + "/" + photos.get(position));
			imageview.setImageBitmap(bitmap);
			return imageview;
		}
	}
}